<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="box.css">
	<title>粘贴板</title>
</head>
<body style="background-image: url(https://www.w3school.com.cn/i/photo/tree.png);
  background-repeat: no-repeat;">
<div class="box1">
	<div class="headbox">
		<h2>中转粘贴板</h2>
	</div>
	<form  method="post" action="index.php">
		<div class="area" >
				<textarea autofocus name="content" id="input" rows="10" cols="50" class="text_area"></textarea>
		</div>
		<div class="chose">
				<input type="submit" name="delete" value="删除" style="float: left;">
				<?php if(array_key_exists("delete",$_POST)){
				echo '<span class="span1">删除成功!</span>';
				button1();} ?>
				<input type="submit" name="submit" value="提交" onclick="fresh()" style="float: right; ">
		</div>		
	</form>
	<div class="success" id="ttt">
		<?php 
			$content = "";
			$content = $_POST["content"];
			if(array_key_exists("submit",$_POST)){
			echo "<br/><h2>提交成功!</h2>";
			echo "提交的内容是："."<br/><span class='span2'>".$content."</span><br/>"."<hr/>";
			button2();}
		?>
	</div>
	<div class="chose" style="clear: both; padding-top: 5px;">
		历史记录：<hr/>
	</div>
	<div>
		<table class="chose ">
			<?php
			$memary = (file("history.txt"));
			$num = count($memary);
			for ($i=$num;$i>0;$i--) {
			echo '<div class= "xiang"><tr><td><input type="button" value="复制" onclick="copyText('.$i;
			echo ')"></td><td class= "item"  id = "'.$i;
			echo '">'.$memary[$i-1]."</div></td></tr>" ; }
			?>		
		</table>
	</div>
	<footer>
		<br/><div><hr class="hr1"></div>
		<p style="text-align: center;">Created By Mr.K</p><!--QQ:2067832196-->
	</footer>	
</div>

</body>
<?php

function button1(){
	$reuse = (file("history.txt"));//按钮1
	array_pop($reuse);
	$num = count($reuse);
	$myfile = fopen("history.txt","w");
	$myfile2 = fopen("history.txt","a+");
	fwrite($myfile,"");
	for ($i=0; $i <$num ; $i++) { 
		fwrite($myfile2,$reuse[$i]);
	}
	
	fclose($myfile);
}
function button2(){

	global $content;
	$file=fopen("history.txt","a+");
    if ($content != "" ) {;
    $content = htmlspecialchars($content);
    $hg = strpos($content,"\n");
    if ($hg != "FALSE"){
      $content =str_ireplace(PHP_EOL,"<br/>",$content); }
    $txt =$content."\n";
    fwrite($file, $txt);
    fclose($file);
    }else{ 	fclose($file);}
	 $file=fopen("history.txt","a+");
}
?>
<script>

function copyText(number) {
    	var numm = number;
    	var empty = "";
      var text = document.getElementById(""+numm+"").innerText;
      var input = document.getElementById("input");
      input.value = text; // 修改文本框的内容
      input.select(); // 选中文本
      document.execCommand("copy"); // 执行浏览器复制命令
      input.value = empty;//清空文本框内容
      alert("复制成功");
    }
function jump() { 
           window.location.href="index.php"; 
         } 	
var old = <?php
$all = file("history.txt");
$countall = count($all);
echo($countall);
?>	;
function fresh(){             
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      var now=this.responseText;
	  if (old != now){
		jump();
		old = now;
	  }
    }
  };
  xhttp.open("GET", "service.php?u=1", true);
  xhttp.send();}
  setInterval(function(){fresh(); }, 1500);//定时执行
</script>
</html>